package com.igeek.compus.backend.product.controller;

import com.igeek.compus.backend.product.entity.Product;
import com.igeek.compus.backend.product.service.ProductService;
import com.igeek.compus.backend.system.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    ProductService productService;

    @ResponseBody
    @RequestMapping("deleteProductList.do")
    public Map<String, Object> deleteProductList(String productIdList){
        Map<String,Object> result=new HashMap<>();

        List<String> productIds = Arrays.asList(productIdList.substring(1, productIdList.length() - 1).split(","));

        // 2.执行业务
        try {
            if (productService.deleteProductList(productIds) > 0) {
                result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            } else {
                result.put(Constants.RETCODE, Constants.ERROR_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }

        // 3.返回结果
        return result;

    }

    @ResponseBody
    @RequestMapping("list.do")
    public Map<String ,Object> list(Product product){
        Map<String,Object> result=new HashMap<>();
        try {
            // 业务代码，菜单信息查询
            List<Product> productList = productService.getProductList(product);
            result.put("data", productList);
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }
        return result;
    }

    @RequestMapping("toProductEdit.do")
    public ModelAndView toSellerEdit(Integer id){
        Map<String ,Object> result=new HashMap<>();
        System.out.println(id);
        Product product=new Product();
        product.setId(id);
        List<Product> productList = productService.getProductList(product);

        result.put("id",productList.get(0).getId());
        result.put("status",productList.get(0).getStatus());
        result.put("productName",productList.get(0).getProductName());
        result.put("description",productList.get(0).getDescription());
        result.put("price",productList.get(0).getPrice());

        return new ModelAndView("/product/product_edit",result);
    }

    @ResponseBody
    @RequestMapping("updateProduct.do")
    public Map<String, Object> updateProduct(Product product){
        Map<String,Object> result=new HashMap<>();
        try {
            // 业务代码，修改商品信息
            productService.updateProduct(product);
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("deleteProductById.do")
    public Map<String, Object> deleteProductById(Integer id){
        Map<String,Object> result=new HashMap<>();

        try {
            // 业务代码，修改商品信息
            int i = productService.deleteProductById(id);
            System.out.println(i);
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }
        return result;
    }

    /**
     * 先判断传进来的卖家编号是否存在，如果不存在添加商品失败，
     * @param product
     * @return
     */
    @ResponseBody
    @RequestMapping("addProduct.do")
    public Map addProduct(Product product){
        Map<String,Object> result=new HashMap<>();

        Product product1=new Product();
        product1.setSellerId(product.getSellerId());

        try {
            if(productService.getProductList(product1).size()>0){
                productService.addProduct(product);
                result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            }else {
                result.put(Constants.RETCODE, Constants.ERROR_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            }

        }catch (Exception e){
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }
       return result;
    }


}
